package com.danger.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Button;
import android.widget.TextView;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.res.DimensionRes;
import org.androidannotations.annotations.res.StringArrayRes;
import org.androidannotations.annotations.res.StringRes;

/**
 * 用注解的方法是不能用private修饰的
 */
@EActivity(R.layout.activity_main)
public class MainActivity extends AppCompatActivity {

    /**
     * @ViewById 该标签用于替换findViewById的操作，但有一点需要注意，不能将view声明为private
     * 如果声明的属性变量的名字与id相同，则不需要在ViewById中指定id
     */
    @ViewById
    Button button;
    @ViewById
    Button thrid_button;
    @ViewById
    Button background_button;
    @ViewById
    TextView textView1;
    @ViewById(R.id.textView2)
    TextView textView2;
    @ViewById(R.id.textView3)
    TextView textView3;

    /**
     * @StringRes(R.string.name) 该标签用于获取资源文件中的数据
     */
    @StringRes(R.string.name)
    String name;

    /**
     * @StringArrayRes(R.array.name_array) 该标签用于获取资源文件中的array
     */
    @StringArrayRes(R.array.name_array)
    String[] name_array;

    /**
     * @DimensionRes(R.dimen.fontSize) 该标签用于获取资源文件中的dimension值
     */
    @DimensionRes(R.dimen.fontSize)
    float fontSize;

    public static final String NAME = "name";
    public static final String AGE = "age";

    /**
     * 三种初始化方法；
     * 1.通过@ViewById(R.id.textView),此处id与变量名不需一致
     * 2.通过@ViewById,此处id与变量名需一致
     * 3.通过ViewsById:
     *
     * @ViewsById({R.id.textView1,R.id.textView2}) List<TextView> list;
     */

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i("danger", "Main Thread id = " + Thread.currentThread().getId());
    }


    /**
     * @Click(R.id.button) 该标签用于为view添加点击事件，前置条件是需要用@ViewById初始化
     * 也可通过集合的方式为View添加点击事件
     * @Click({R.id.textView1,R.id.textView2})
     */
    @Click(R.id.button)
    public void startActivity() {
        Intent intent = new Intent(MainActivity.this, SecondActivity_.class);
        intent.putExtra(NAME, "danger");
        intent.putExtra(AGE, "17");
        startActivity(intent);
    }

    @Click(R.id.thrid_button)
    public void toThridActivity() {
        Intent intent = new Intent(MainActivity.this, ThridActivity_.class);
        startActivity(intent);
    }

    /**
     * @Background 该标签将该方法放入后台线程中执行
     */
    @Background
    public void doSomething() {
        Log.i("danger", "Thread id = " + Thread.currentThread().getId());
        updateUI();
    }

    /**
     * @UiThread 与handler进行交互，在后台线程中将更新UI的操作递交给handler完成
     * 在一个方法中前加上@UiThread 标签，则该方法会被放入主线程中执行
     */
    @UiThread
    public void updateUI() {
        textView3.setText("I have been updated!!!");
    }

    @Click(R.id.background_button)
    public void backgroundClick() {
        doSomething();
    }

    /**
     * @AfterViews 该标签用于在view初始化（findViewById）之后调用
     */
    @AfterViews
    public void setTextView() {
        textView1.setText("TextView1");
        textView2.setText("TextView2");
        textView3.setText(name);
        textView3.setTextSize(fontSize);
    }
}